// pages/module/search/search.js
import regeneratorRuntime from '../../../utils/runtime.js';
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import shareFun from '../../../utils/shareFun.js';
import {
  fetchOpenId,
  query_hot,
  query_his,
} from '../../../utils/imapi.js';

var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex:0,
    keyword: '',
    sliderLeft:55,
    sliderOffset:155,
    tabs:[
      {"id":1,"name":"全网","p":"","is_selected":1},
      {"id":2,"name":"拼多多","p":"PDD","is_selected":1},
      {"id":3,"name":"唯品会","p":"VIP","is_selected":1},
      {"id":4,"name":"京东","p":"JD","is_selected":1},
      {"id":5,"name":"淘宝","p":"TB","is_selected":1}
    ],
    data: [],
    tagsList: [],
    hot_series:[],
	hot_series_1:[],
    chooseIndex: -1,
    recordList: [],
	recordList_1: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    var that = this;
  
    shareFun.hidenShareMenu();
    let recordList = [];
    try {
      recordList = await getData(dataKey.recordList);
    } catch (error) {
      await setData(dataKey.recordList,[]);
    }
    recordList = recordList.slice(0, 19);
    that.setData({
      recordList
    })
	
	var res=await query_hot({page_size:1,page_index:10});
	console.log(res);
	var res1=await query_his({page_size:1,page_index:10});
	console.log(res1);
	
	that.setData({
	  hot_series_1:res.data,
	  recordList_1:res1.data
	});
  },
  onTabClick(event) {
    let index = event.detail.index;
    this.data.tabIndex = index
  },
  chooseItem(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var obj = that.data.data[index];
    obj.type = 1;
    obj.is_selected = !obj.is_selected;
    if (obj.is_selected) {
      that.data.tagsList.push(obj);
      if (obj.hot_series[0]) {
        obj.ind = index;
        that.data.seriesList.unshift(obj)
      }
      that.setData({
        currentIndex: index,
        seriesList: that.data.seriesList,
        data: that.data.data,
        tagsList: that.data.tagsList
      })
    } else {

      that.data.seriesList.forEach((item, n) => {
        if (item.ind == index) {
          that.data.seriesList.splice(n, 1);
        };
      });

      that.data.data[index].hot_series.forEach((item, n) => {
        item.is_selected = 0;
      });


      var tagsList = that.data.tagsList.filter(function (item) {
        if(item.id == id){
          item.is_hide = 0;
        }
        return item.id != id && item.parent_id != id
      })

      that.setData({
        seriesList: that.data.seriesList,
        data: that.data.data,
        tagsList: tagsList
      })
    }

  },

  category(){
    wx.navigateTo({
      url: '/pages/tabbar/classification/classification',
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },


  chooseSeries(e) {
	  console.log(e);
    var that = this;
    var index = e.currentTarget.dataset.index;
    var p = this.data.tabs[this.data.tabIndex].p
	if(!this.data.hot_series){
		var keyword = that.data.hot_series[index];
	}else{
		var keyword = e.currentTarget.dataset.keyword;
	}
    wx.navigateTo({
      url: '/pages/module/search/searchResult/searchResult?keyword=' + keyword + '&p=' + p + '&tabIndex=' + this.data.tabIndex
    })
  },


  getWord(e) {
    this.setData({
      keyword: e.detail.value
    })
  },

  async startSearch() {
    // if(!this.data.keyword){
    //   wx.showToast({
    //     icon:'none',
    //     title: '请输入搜索商品',
    //   })
    //   return;
    // }
    var that = this;
    var keyword = this.data.keyword;
    var p = this.data.tabs[this.data.tabIndex].p
    var recordList = await getData(dataKey.recordList);
    if (recordList.indexOf(keyword) == '-1' && keyword) {
      recordList.unshift(keyword);
      recordList = recordList.slice(0, 19);
      await setData(dataKey.recordList,recordList);
    }


    

    if(app.globalData.is_audit){

      
    if(app.globalData.is_audit){
      // 禁用关键词搜索判断
      if(app.is_disable_keywords(keyword)){

        wx.showToast({
          title: '暂不支持该关键词搜索',
        })
        return 

      }
          
   
    }
      
    }

    wx.navigateTo({
      url: '/pages/module/search/searchResult/searchResult?keyword=' + keyword + '&p=' + p + '&tabIndex=' + this.data.tabIndex
    })
  },

  delete() {
    var that = this;
    wx.showModal({
      title: '是否删除记录',
      async success(e) {
        if (e.confirm) {
          await setData(dataKey.recordList, [])
          that.setData({
            recordList: []
          })
        }
      }
    })
  },

  goResult(e) {
	  console.log(e);
	  console.log(this.data.recordList);
    var that = this;
    var item = e.currentTarget.dataset.index;
    //var keyword = that.data.recordList[index];
    var p = this.data.tabs[this.data.tabIndex].p
	// if(this.data.recordList){
	// 	console.log('1');
	// 	var keyword = that.data.recordList[index];
	// }else{
	// 	console.log(2);
	// 	var keyword = that.data.recordList_1[index];
	// }
	// console.log(keyword);
    wx.navigateTo({
      url: '/pages/module/search/searchResult/searchResult?keyword=' + item + '&p=' + p + '&tabIndex=' + this.data.tabIndex
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    console.log('app.globalData.is_audit',app.globalData.is_audit)
    if (app.globalData.configs.taobao_search_enable=='0' || app.globalData.is_audit){
      this.setData({
        tabs:[
          {"id":1,"name":"全网","p":"","is_selected":1},
          {"id":2,"name":"拼多多","p":"PDD","is_selected":1},
          {"id":3,"name":"唯品会","p":"VIP","is_selected":1},
          {"id":4,"name":"京东","p":"JD","is_selected":1},
        //  {"id":5,"name":"淘宝","p":"TB","is_selected":1},
        ]
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})